package com.boot.security.server.service.impl;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URLDecoder;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.boot.security.server.dao.FileInfoDao;
import com.boot.security.server.model.FileInfo;
import com.boot.security.server.service.FileService;
import com.boot.security.server.utils.FileUtil;

import javax.imageio.ImageIO;

@Service
public class FileServiceImpl implements FileService {

	private static final Logger log = LoggerFactory.getLogger("adminLogger");

	@Value("${files.path}")
	private String filesPath;
	@Autowired
	private FileInfoDao fileInfoDao;

	@Override
	public FileInfo save(MultipartFile file) throws IOException {
		String fileOrigName = file.getOriginalFilename();
		if (!fileOrigName.contains(".")) {
			throw new IllegalArgumentException("缺少后缀名");
		}

		String md5 = FileUtil.fileMd5(file.getInputStream());
		FileInfo fileInfo = fileInfoDao.getById(md5);
		if (fileInfo != null) {
			File imageFile = new File(fileInfo.getPath());
			if (imageFile.exists()){
				fileInfoDao.update(fileInfo);
				return fileInfo;
			}else {
				FileUtil.saveFile(file, fileInfo.getPath());
				return fileInfo;
			}

		}

		fileOrigName = fileOrigName.substring(fileOrigName.lastIndexOf("."));
		String pathname = FileUtil.getPath() + md5 + fileOrigName;
		String fullPath = filesPath + pathname;


		FileUtil.saveFile(file, fullPath);

		long size = file.getSize();
		String contentType = file.getContentType();

		fileInfo = new FileInfo();
		fileInfo.setId(md5);
		fileInfo.setContentType(contentType);
		fileInfo.setSize(size);
		fileInfo.setPath(fullPath);
		fileInfo.setUrl(pathname);
		fileInfo.setType(contentType.startsWith("image/") ? 1 : 0);

		fileInfoDao.save(fileInfo);

		log.debug("上传文件{}", fullPath);

		return fileInfo;

	}

	@Override
	public void delete(String id) {
		FileInfo fileInfo = fileInfoDao.getById(id);
		if (fileInfo != null) {
			String fullPath = fileInfo.getPath();
			FileUtil.deleteFile(fullPath);

			fileInfoDao.delete(id);
			log.debug("删除文件：{}", fileInfo.getPath());
		}
	}

	@Override
	public FileInfo chatImgSave(FileInfo fileInfo) throws IOException{

		String path = fileInfo.getPath();
		String url = fileInfo.getUrl();
		File imageFile = new File(URLDecoder.decode(path, "utf-8"));
		//800*450
		int st = path.indexOf(".");
		String suffix = path.substring(st);
		String n800Path = path.substring(0, st) + "_chat" + suffix;

		int st1 = url.indexOf(".");
		String suffix1 = url.substring(st1);
		String url1 = url.substring(0, st1) + "_chat" + suffix1;

		Image image = ImageIO.read(imageFile);
		BufferedImage image720 = FileUtil.compressImageScadvert(image, 450, 300, suffix);
		ImageIO.write(image720, path.substring(st + 1), new File(n800Path));

		fileInfo.setUrl(url1);
		return fileInfo;
	}


}
